package com.nakamachizu.model.session;

import java.io.Serializable;
import java.util.Date;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;
import org.slim3.util.StringUtil;

import com.google.appengine.api.datastore.Key;
import com.nakamachizu.constant.MixiConstant;
import com.nakamachizu.util.NakamachizuStringUtil;

@Model(schemaVersion = 1)
public class MixiSessionData implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;
    
    private String accessKey;
    
    private String mapKey;
    
    private String ownerId;
    
    private String ownerDisplayName;
    
    private String viewerId;
    
    private String viewerDisplayName;
    
    private Boolean friend;
    
    private String appId;
    
    private Date entryDate;
    
    private Date expireDate;

    private String viewPassword;
    
    public boolean isOwner(){
        if(mixiIdToNumeric(ownerId).equals(mixiIdToNumeric(viewerId))){
            return true;
        }else{
            return false;
        }
    }
    
    public String getAdminMailAddress(){
        return mixiIdToNumeric(ownerId) + MixiConstant.MIXI_MAIL_ADDRESS();
    }
    
    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MixiSessionData other = (MixiSessionData) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    public String getOwnerId() {
        return mixiIdToNumeric(ownerId);
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getViewerId() {
        return mixiIdToNumeric(viewerId);
    }

    public void setViewerId(String viewerId) {
        this.viewerId = viewerId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setEntryDate(Date entryDate) {
        this.entryDate = entryDate;
    }

    public Date getEntryDate() {
        return entryDate;
    }
    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }
    public Date getExpireDate() {
        return expireDate;
    }

    public void setViewPassword(String viewPassword) {
        this.viewPassword = viewPassword;
    }

    public String getViewPassword() {
        return viewPassword;
    }

    public String getOwnerDisplayName() {
        return ownerDisplayName;
    }

    public void setOwnerDisplayName(String ownerDisplayName) {
        this.ownerDisplayName = ownerDisplayName;
    }

    public String getViewerDisplayName() {
        return viewerDisplayName;
    }

    public void setViewerDisplayName(String viewerDisplayName) {
        this.viewerDisplayName = viewerDisplayName;
    }

    public void setFriend(Boolean friend) {
        this.friend = friend;
    }

    public Boolean getFriend() {
        return friend;
    }

    public void setMapKey(String mapKey) {
        this.mapKey = mapKey;
    }

    public String getMapKey() {
        return mapKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public String mixiIdToNumeric(String mixiId){
        return NakamachizuStringUtil.mixiIdToNumeric(mixiId);
    }

}
